package com.cenntro.oss.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.curator.shaded.com.google.common.collect.ImmutableMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cenntro.auth.mapper.SysSooidMapper;
import com.cenntro.auth.model.dbo.SysSooidDO;
import com.cenntro.common.base.BaseServiceImpl;
import com.cenntro.common.util.ConvertorUtils;
import com.cenntro.oss.mapper.DevCarMapper;
import com.cenntro.oss.mapper.DevDeviceStaticMapper;
import com.cenntro.oss.mapper.DevTransferRecordLogMapper;
import com.cenntro.oss.mapper.SysAgentMapper;
import com.cenntro.oss.model.dbo.DevCarDO;
import com.cenntro.oss.model.dbo.DevDeviceStaticDO;
import com.cenntro.oss.model.dbo.DevTransferRecordLogDO;
import com.cenntro.oss.model.dbo.SysAgentDO;
import com.cenntro.oss.model.qo.DevTransferQO;
import com.cenntro.oss.model.vo.TransferListVO;
import com.cenntro.oss.service.DevTransferRecordLogService;
import com.google.common.base.Function;
import com.google.common.collect.Lists;

/**
 * <p>
 * 车型表 服务实现类
 * </p>
 *
 * @author
 * @since 2018-09-17
 */
@Service
public class DevTransferRecordLogImpl extends BaseServiceImpl<DevTransferRecordLogMapper>
		implements DevTransferRecordLogService {

	@Resource
	private DevTransferRecordLogMapper devTransferLogMapper;
	@Resource
	private DevCarMapper devCarMapper;
	@Resource
	private DevDeviceStaticMapper devDeviceStaticMapper;
	@Resource
	private SysSooidMapper sysSooidMapper;
	@Resource
	private SysAgentMapper sysAgentMapper;

	/**
	 * 车辆过户列表
	 */
	@Override
	public List<TransferListVO> selectTransferList(Integer agentId) {
		List<DevTransferRecordLogDO> listDO = devTransferLogMapper.selectTransferList(agentId);
		List<TransferListVO> convertList = ConvertorUtils.deepConvertList(listDO, TransferListVO.class);
		return convertList;
	}

	/**
	 * 新增过户车辆
	 */
	@Transactional
	@Override
	public int insertTransfer(DevTransferQO qo) {
		DevTransferRecordLogDO logDO = new DevTransferRecordLogDO();
		String devIds = qo.getDevId();
		String[] split = devIds.split(",");
		List<Integer> idsList = Lists.transform(Lists.newArrayList(split), new Function<String, Integer>() {
			public Integer apply(String input) {
				return NumberUtils.toInt(input);
			};
		});

		for (int i = 0; i < idsList.size(); i++) {
			DevDeviceStaticDO devDO = devDeviceStaticMapper.selectDevOne(idsList.get(i), qo.getOldAgentId());
			if (devDO != null) {
				DevDeviceStaticDO staticDO = devDeviceStaticMapper.selectById(idsList.get(i));
				SysSooidDO sooidDO = sysSooidMapper.selectOne(ImmutableMap.of("userName", qo.getUserName()));
				SysAgentDO agentDO = sysAgentMapper.selectById(staticDO.getAgentId());
				DevCarDO carDO = devCarMapper.selectById(staticDO.getCarId());
				if (staticDO == null || sooidDO == null || agentDO == null || carDO == null) {
					throw new RuntimeException("查询为空");
				}
				logDO.setOperatorName(sooidDO.getRealName());
				logDO.setOperatorSooid(sooidDO.getId());
				logDO.setCarType(carDO.getTitle());
				logDO.setCompany(agentDO.getCompany());
				logDO.setCarNo(staticDO.getCarNo());
				logDO.setDevSn(staticDO.getDevSn());
				logDO.setImei(staticDO.getImei());
				logDO.setOldAgentId(qo.getOldAgentId());
				logDO.setNewAgentId(qo.getNewAgentId());
				logDO.setOperatorName(qo.getUserName());
				logDO.setCdt(new Date());
				devTransferLogMapper.insert(logDO);
			} else {
				return 0;
			}
		}
		return -1;
	}

}
